package com.erpanderp.wam.ac.model.service.reimbursement;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.reimbursement.BoardingExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.LodgingExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TransportExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelMiscExpense;
import com.erpanderp.wam.ac.model.entity.travel.TravelRequest;
import com.erpanderp.wam.cm.model.entity.AdditionalSkill;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class TravelReimbursementDAO {
	static Logger logger = Logger.getLogger(TravelReimbursementDAO.class.getName());
			
	public List<TravelRequest> getAllTravelRequestsByEmployeeId(Long employeeId){
		logger.info("I(getAllTravelRequestsByEmployeeId) AM CALLED");
		List<TravelRequest> travelRequestList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
		    String queryString = "from TravelRequest t where t.employee.employeeId =:employeeId";
		    Query query = session.createQuery(queryString).setLong("employeeId", employeeId);
		    travelRequestList = query.list();
		}catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
		return travelRequestList;		
	}
	public List<TravelExpense> getAllTravelExpensesByEmployeeId(Long employeeId){
		logger.info("I(getAllTravelExpensesByEmployeeId) AM CALLED");
		List<TravelExpense> travelExpenseList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
		    String queryString = "from TravelExpense te where te.employee.employeeId =:employeeId";
		    Query query = session.createQuery(queryString).setLong("employeeId", employeeId);
		    travelExpenseList = query.list();
		}catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
		return travelExpenseList;
		
	}

	/**
	 * 
	 * @param travelRequestId
	 * @return
	 */
	public List<TravelExpense> getTravelExpenses(Long travelRequestId){
		List<TravelExpense> travelExpenseList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
		    String queryString = "from TravelExpense te where te.travelRequest.travelRequestId =:travelRequestId";
		    Query query = session.createQuery(queryString).setLong("travelRequestId", travelRequestId);
		    travelExpenseList = query.list();
		    session.getTransaction().commit();
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return travelExpenseList;
		
	}
	public TravelExpense getTravelExpenseByTravelExpenseCode(String travelExpenseCode){
		TravelExpense travelExpense = null;
		try {
 			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 			session.beginTransaction();
 			session.setFlushMode(FlushMode.COMMIT);
 			Query query = session.createQuery("from TravelExpense te where te.travelExpenseCode = :travelExpenseCode");
 			System.out.println(query == null);
 			query.setCacheable(false).setString("travelExpenseCode",travelExpenseCode);
 			travelExpense = (TravelExpense) query.uniqueResult();
 			session.getTransaction().commit();
 			
 			return travelExpense;
 		} catch (Exception e) {
 			e.printStackTrace();
 			return null;
 		}
		
	}
	 public String getLastGeneratedTravelExpenseFormCode(String employeeCode){	    	
	  Long count = null;	 		
	 	try{
	 			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	 			session.getTransaction().begin();
	 			String queryString = "select count(*) from TravelExpense te where te.employee.employeeCode=:employeeCode";
	
	 			Query query = session.createQuery(queryString);
	 			query.setCacheable(false).setString("employeeCode",employeeCode);;
	 			count = (Long) query.uniqueResult();
	 			session.getTransaction().commit();
	 		} catch (HibernateException e) {
	 			e.printStackTrace();
	 		}
	 		
	 		count = count+1;
	 		return count.toString();	 		
	     }
	 public TravelExpense createTravelExpense(TravelExpense travelExpense) {
			try {
				Session session = HibernateUtil.getSessionFactory()
						.getCurrentSession();
				session.getTransaction().begin();
				System.out.println("FLUSH MODE: " + session.getFlushMode());
				session.setFlushMode(FlushMode.COMMIT);
				session.saveOrUpdate(travelExpense);
				session.getTransaction().commit();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return travelExpense;
		}
	 
	 
	 //DELETE METHODS
	 public TransportExpense deleteTransportExpenseById(TransportExpense transportExpense)
		{
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				session.delete(transportExpense);
				session.getTransaction().commit();
				return transportExpense;
				
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
		}
	 
	 public LodgingExpense deleteLodgingExpenseById(LodgingExpense lodgingExpense)
		{
		 try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				session.delete(lodgingExpense);
				session.getTransaction().commit();
				return lodgingExpense;
				
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}	
		}
	 
	 public BoardingExpense deleteBoardingExpenseById(BoardingExpense boardingExpense)
		{
		 try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				session.delete(boardingExpense);
				session.getTransaction().commit();
				return boardingExpense;
				
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
		}
	 
	 public TravelMiscExpense deleteTravelMiscExpenseById(TravelMiscExpense travelMiscExpense)
		{
		 try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				session.delete(travelMiscExpense);
				session.getTransaction().commit();
				return travelMiscExpense;
				
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}	
		}
	 
	 //For getting inprocess records for recommender
	 public List<TravelExpense> getTravelExpenseRequestsByRecommenderId(Long employeeId, String status) {
			List<TravelExpense> travelExpenses = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				
				String queryString = "from TravelExpense te where te.recommenderBy.employeeId = :employeeId";
				if(status != null && !status.equals(""))
					queryString += " and te.recommenderStatus = :status";
				
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setLong("employeeId",	employeeId);
				if(status != null && !status.equals(""))
					query.setString("status", status);
				
				travelExpenses = (List<TravelExpense>) query.list();
				session.getTransaction().commit();
				return travelExpenses;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
	 
	//For getting inprocess records for approver
		 public List<TravelExpense> getTravelExpenseRequestsByApproverId(Long employeeId, String status) {
				List<TravelExpense> travelExpenses = null;
				try {
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					session.beginTransaction();
					session.setFlushMode(FlushMode.COMMIT);
					
					String queryString = "from TravelExpense te where te.approvedBy.employeeId = :employeeId";
					if(status != null && !status.equals(""))
						queryString += " and te.approverStatus = :status";
					
					Query query = session.createQuery(queryString);
					query.setCacheable(false).setLong("employeeId",	employeeId);
					if(status != null && !status.equals(""))
						query.setString("status", status);
					
					travelExpenses = (List<TravelExpense>) query.list();
					session.getTransaction().commit();
					return travelExpenses;
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}
	 
	 public static void main(String a[]){
for(TravelMiscExpense expense : new TravelReimbursementDAO().getTravelExpenseByTravelExpenseCode("E50005-TF2-EF1").getTravelMiscExpenses()){
	System.out.println(expense.getTravelMiscExpenseId());
}
	 }

}
